package com.core;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

public class ConnectionFactory {

	
	public static URLConnection createURLConnection(URL url){
		URLConnection connection = null;
		int port = 8080;
		String proxyIP = DoubanProperties.getInstance().getProperty("proxy");

		if(proxyIP!=null && proxyIP.matches( "^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}" ) == false){
			try{
				port = (new Integer(1)).parseInt(DoubanProperties.getInstance().getProperty("port"));
				Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(DoubanProperties.getInstance().getProperty("proxy"), port));
				connection = (HttpURLConnection)url.openConnection(proxy);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}else{
			try {
				connection = url.openConnection();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return connection;
	}
	
}
